var express = require('express');
var router = express.Router();
var mongoose = require('mongoose')
var mutiparty = require('multiparty')
var User = require('../models/user')
var Goods = require('../models/goods')
var dbUrl = 'mongodb://root:1234@127.0.0.1:27017/mollGoods?authSource=admin' //数据库网址



//连接MongoDB数据库
mongoose.connect(dbUrl)

mongoose.connection.on("connected",function () {
  console.log("MongoDB connected success")
})

mongoose.connection.on("error",function () {
  console.log("MongoDB connected error")
})

mongoose.connection.on("disconnected",function () {
  console.log("MongoDB connected disconnected")
})

/* GET home page. */
router.post("/",function (req,res,next) {

  let params = req.body
  console.log(params)
  //参数

  let page = params.page?parseInt(params.page):1
  let pageSize = params.size?parseInt(params.size):10
  let sort = params.sort?parseInt(params.sort):1
  let priceLevel = params.priceLevel?params.priceLevel:0

  let skip = (parseInt(page)-1)*pageSize
  let param = {}

  //将上传数据保存到数据库
  var priceGt = '', priceLte = ''
  if (priceLevel != 'all') {
    switch (priceLevel) {
      case 0:priceGt = 0;priceLte=100;break;
      case 1:priceGt = 100;priceLte=500;break;
      case 2:priceGt = 500;priceLte=1000;break;
      case 3:priceGt = 1000;priceLte=1500;break;
      case 4:priceGt = 1500;priceLte=10000;break;
    }
    console.log('gt is'+priceGt + 'lte is'+priceLte)
    param = {
      salePrice: {
        $gt: priceGt,
        $lte: priceLte
      }
    }
  }
  let goodsModel = Goods.find(param).skip(skip).limit(pageSize)
  goodsModel.sort({'salePrice':sort})
  goodsModel.exec(function (err,doc) {
    if (err) {
      res.json({
        status: "0",
        msg: err.message
      })
    }else {
      res.json({
        status: "1",
        msg: '',
        result: {
          count: doc.length,
          list: doc
        }
      })
    }
  })
})

router.post("/addCart",(req,res,next)=>{
  var userId = '100000077', productId = req.body.productId

  User.findOne({
    userId: userId
  },(err,userDoc)=>{
    if (err) {
      res.json({
        status: "0",
        msg: err.message
      })
    }else {
      if (userDoc){
        let goodsItem = ''
        userDoc.cartList.forEach(function (item,index) {
          if (item.productId == productId) {
            goodsItem = item
            item.productNum = parseInt(item.productNum)+1
          }
        })
        if (goodsItem) {
          userDoc.save(function (err2,doc2) {
            if(err2) {
              res.json({
                status: "0",
                msg: err2.message
              })
            }else {
              res.json({
                status: "1",
                msg: "",
                result: "suc"
              })
            }
          })
        }else {
          Goods.findOne({productId:productId},(err,goodsDoc)=>{
            if(err) {
              res.json({
                status: "0",
                msg: err.message
              })
            }else {
              if(goodsDoc) {
                var goods = goodsDoc
                goods.productNum = 1
                goods.checked = 1

                console.log(`goodsDoc is:${goods}`)
                userDoc.cartList.push(goods)
                userDoc.save(function (err2,doc2) {
                  if(err2) {
                    res.json({
                      status: "0",
                      msg: err2.message
                    })
                  }else {
                    res.json({
                      status: "1",
                      msg: "",
                      result: "suc"
                    })
                  }
                })
              }
            }
          })
        }
      }
    }
  })

})

module.exports = router
